#!/bin/bash
#
# Pings a host every 5 minutes and raises a condition alert if the host is not reachable
#
# usage: ping-monitor <hostname> [condition-id]

host=${1:?no hostname given}
condition=${2:-$host}

last_query=0

while true; do
	error=$(ping -c 1 $host 2>&1 > /dev/null)
	this_query=$?
	
	if [ $this_query != $last_query ]; then
		if [ $this_query == 0 ]; then
			condition clear "$condition"
		else
			condition alert "$condition" "$error"
		fi
		
		last_query=$this_query
	fi
	sleep 300
done

